import React from 'react'
import './index.scss';

function createToastBox () {
  let body = document.querySelector('body');
  let toastBox = document.querySelector('.toasts');
  if (!toastBox) {
    let el = document.createElement('div');
    el.className = 'toasts';
    body.appendChild(el);
  }
}

function Toast ({ delay, text, freeze=true }) {
  if (Toast.freeze ) return;
  Toast.freeze = freeze;

  createToastBox();
  let toasts = document.querySelector('.toasts');
  let el = document.createElement('div');
  el.className = 'toast';
  el.innerHTML = text;
  toasts.appendChild(el);
  
  setTimeout(() => {
    toasts.removeChild(el);
    Toast.freeze = false;
  }, delay || 2000);
}

export default Toast;
